HRESULT MSMQSendMessage(LPSTR pszQueueName, LPSTR pszComputerName,
LPSTR pszLabel, LPSTR pszMessageBody, size_t msgsize) {
#define NUMBEROFPROPERTIES 5
DWORD cPropId = 0;
HRESULT hr;
HANDLE hQueue;
WCHAR *wszQueueName;
WCHAR *wszComputerName;
WCHAR *wszLabel;
MQMSGPROPS msgProps;
MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
HRESULT aMsgStatus[NUMBEROFPROPERTIES];
WCHAR * wszFormatName;
DWORD dwBodyType = VT_BSTR;
wszQueueName = Ansi2Unicode(pszQueueName, NULL);
wszComputerName = Ansi2Unicode(pszComputerName, NULL);
wszLabel = Ansi2Unicode(pszLabel, NULL);
aMsgPropId[cPropId] = PROPID_M_LABEL;
aMsgPropVar[cPropId].vt = VT_LPWSTR;
aMsgPropVar[cPropId].pwszVal = wszLabel;
cPropId++;
aMsgPropId[cPropId] = PROPID_M_BODY;
aMsgPropVar[cPropId].vt = VT_VECTOR | VT_UI1;
aMsgPropVar[cPropId].caub.pElems = pszMessageBody;
aMsgPropVar[cPropId].caub.cElems = msgsize;
cPropId++;
aMsgPropId[cPropId] = PROPID_M_BODY_TYPE;
aMsgPropVar[cPropId].vt = VT_UI4;
aMsgPropVar[cPropId].ulVal = VT_EMPTY;
cPropId++;
msgProps.cProp = cPropId;
msgProps.aPropID = aMsgPropId;
msgProps.aPropVar = aMsgPropVar;
msgProps.aStatus = aMsgStatus;
wszFormatName = (WCHAR *)malloc((wcslen(wszComputerName) + wcslen(wszQueueName) + 12) * 2);
swprintf(wszFormatName,
L"DIRECT=OS:%s\\%s",
wszComputerName,
wszQueueName
);
hr = MQOpenQueue(
wszFormatName,
MQ_SEND_ACCESS,
MQ_DENY_NONE,
&hQueue
);
free(wszFormatName);
if (FAILED(hr)) {
printf("MQOpenQueue failed.\n");
return hr;
}
hr = MQSendMessage(
hQueue,
&msgProps,
MQ_NO_TRANSACTION
);
if (FAILED(hr)) {
printf("MQSendQueue failed.\n");
return hr;
}
GlobalFree(wszQueueName);
GlobalFree(wszComputerName);
GlobalFree(wszLabel);
hr = MQCloseQueue(hQueue);
if (FAILED(hr)) {
printf("MQCloseQueue failed.\n");
return hr;
}
return hr;
} |